package org.android.bbox.activities;

import org.android.bbox.R;
import org.android.bbox.data.NoDatabaseException;
import org.android.bbox.data.Profil;
import org.android.bbox.generic.AbstractActivity;
import org.android.bbox.utils.RId;

import com.example.android.apis.graphics.ColorPickerDialog;

import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ProfilChoice extends AbstractActivity implements OnItemClickListener, ColorPickerDialog.OnColorChangedListener
{
	private String ip;
	private String name;
	private int id;
	private String login;
	private String password;
	private String password_user;
	private boolean save_password;
	private String image;
	private String color;

	private Bitmap color_bitmap;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.chooseAProfil();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id)
	{
		Toast msg = Toast.makeText(this, "Vous avez cliqué sur : " + id + "(" + position + ")", Toast.LENGTH_SHORT);
		msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
		msg.show();
	}

	@Override
	public void colorChanged(int color)
	{
		this.color_bitmap.eraseColor(color);
		((ImageView) this.findViewById(R.id.profil_color)).setImageBitmap(this.color_bitmap);
	}

	private void initForm(final boolean isNew)
	{
		this.setContentView(R.layout.profils_edit);
		this.color_bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
		this.colorChanged(0x00AAAAAA);

		Button btn = (Button) findViewById(R.id.profil_save);
		btn.setText(R.string.label_btn_add);
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v)
			{
				if (ProfilChoice.this.checkForm(isNew))
				{
					ProfilChoice.this.saveProfil(isNew);
				}
			}
		});

		((ImageView) this.findViewById(R.id.profil_color)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v)
			{
				new ColorPickerDialog(ProfilChoice.this, ProfilChoice.this, ProfilChoice.this.color_bitmap.getPixel(1, 1)).show();
			}
		});
	}

	private void addNewProfil()
	{
		this.initForm(true);
	}

	private boolean checkForm(boolean isNew)
	{
//		if (!isNew)
//		{
//			//XXX ....
//			this.id = ...;
//		}

		// save field
		this.ip = RId.getTextViewValue(this, R.id.profil_ip);
		this.name = RId.getTextViewValue(this, R.id.profil_name);
		this.login = RId.getTextViewValue(this, R.id.profil_login);
		this.password = RId.getTextViewValue(this, R.id.profil_password);
		this.password_user = RId.getTextViewValue(this, R.id.profil_password_user);
		this.save_password = RId.getCheckBoxValue(this, R.id.profil_save_password);
		this.image = RId.getTextViewValue(this, R.id.profil_image);
		this.color = RId.getTextViewValue(this, R.id.profil_color);

		// check each field
		boolean valid = RId.switchBackgroundColor(this, R.id.profil_ip, this.ip.equals(""));
		//TODO ip field format : ###.###.###.### ou domain name
		valid = (RId.switchBackgroundColor(this, R.id.profil_name, this.ip.equals("")) && valid);
		valid = (RId.switchBackgroundColor(this, R.id.profil_login, this.ip.equals("")) && valid);

		if (this.save_password)
		{
			RId.switchBackgroundColor(this, R.id.profil_password, this.ip.equals(""));
		}
		else
		{
			this.password = "";
			this.password_user = "";
		}

		return valid;
	}

	private void saveProfil(boolean isNew)
	{
		try
		{
			Profil.create(this, this.ip, this.name,	this.login, this.password, this.password_user, this.save_password, this.image, this.color);
		}
		catch (NoDatabaseException e)
		{
			android.util.Log.e(this.getClass().toString(), e.getMessage(), e);
		}
		this.finish();
	}

	private void chooseAProfil()
	{
		Cursor c = Profil.getAll(this);

		if (c.getCount() > 0)
		{
			this.setContentView(R.layout.profils_list);
			ListView myList = (ListView) findViewById(android.R.id.list);
			myList.setOnItemClickListener(this);
			SimpleCursorAdapter sCAdapter = new SimpleCursorAdapter(this, R.layout.profils_list, c, new String[] { "name" },
					new int[] { android.R.id.text1 });
			myList.setAdapter(sCAdapter);
		}
		else
		{
			this.addNewProfil();
		}
	}
}
